package com.yy.bolg.controller.repair;

import com.yy.bolg.constant.ResultCode;
import com.yy.bolg.entity.RepairBean;
import com.yy.bolg.entity.TestingAccessoryBean;
import com.yy.bolg.entity.TestingBean;
import com.yy.bolg.entity.WorkOrderBean;
import com.yy.bolg.request.RepairRequest;
import com.yy.bolg.request.TestingAccessoryRequest;
import com.yy.bolg.request.TestingRequest;
import com.yy.bolg.resp.RepairResp;
import com.yy.bolg.service.RepairService;
import com.yy.bolg.service.TestingAccessoryService;
import com.yy.bolg.service.TestingService;
import com.yy.bolg.service.WorkOrderService;
import com.yy.bolg.util.CodeGenerateUtil;
import com.yy.bolg.util.RespenseJsonTransformationUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 维修控制器
 * @author enmaoFu
 * @date 2024-11-12
 * RequestMapping("/repair") 是说明这个类的访问地址是 /repair
 */
@Api(tags = {"维修"})
@Controller
@RequestMapping(value = "/repair")
public class RepairController {

    @Autowired
    private RepairService repairService;// 自动载入Service对象
    @Autowired
    private WorkOrderService workOrderService;// 自动载入Service对象

    /**
     * 维修完成
     * 服务器访问路径：http://localhost:8080/bolg/repair/workCompleteRepair
     * 本地访问路径：http://localhost:8111/repair/workCompleteRepair
     * 参数：
     * {"customerCode":"CUS20241219000133","feedbackCode":"FEED20241219000241918787","workOrderCode":"WROR20241219001148","repairContent":"这个问题不大，换了一个开关就好了","repairVideoFile":"视频路径","repairVoiceFile":"音频路径","repairImgFile":"https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500,https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500"}
     */
    @ApiOperation(value = "完成维修", notes = "参数：{\"customerCode\":\"CUS20241219000133\",\"feedbackCode\":\"FEED20241219000241918787\",\"workOrderCode\":\"WROR20241219001148\",\"repairContent\":\"这个问题不大，换了一个开关就好了\",\"repairVideoFile\":\"视频路径\",\"repairVoiceFile\":\"音频路径\",\"repairImgFile\":\"https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500,https://img2.baidu.com/it/u=3434434358,2368357664&fm=253&fmt=auto&app=120&f=JPEG?w=500&h=500\"}")
    @RequestMapping(value = "/workCompleteRepair",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String workCompleteRepair(@RequestBody RepairRequest request){
        String result;
        String repairCode = CodeGenerateUtil.getRepairCode();

        RepairBean repairBean = new RepairBean();
        repairBean.setPsRepairCode(repairCode);
        repairBean.setPsCustomerCode(request.getCustomerCode());
        repairBean.setPsFeedbackCode(request.getFeedbackCode());
        repairBean.setPsWorkOrderCode(request.getWorkOrderCode());
        repairBean.setPsRepairContent(request.getRepairContent());
        repairBean.setPsRepairVideoFile(request.getRepairVideoFile());
        repairBean.setPsRepairVoiceFile(request.getRepairVoiceFile());
        repairBean.setPsRepairImgFile(request.getRepairImgFile());


        WorkOrderBean workOrderBean = new WorkOrderBean();
        workOrderBean.setPsWorkOrderCode(request.getWorkOrderCode());
        workOrderBean.setPsRepairCode(repairCode);
        workOrderBean.setPsWorkOrderRepairState(1);
        workOrderBean.setPsWorkOrderState(5);

        try {
            repairService.add(repairBean);

            workOrderService.workCompleteRepair(workOrderBean);

            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "维修完成", new HashMap());
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

    /**
     * 获取维修结果
     * 服务器访问路径：http://localhost:8080/bolg/repair/getRepairInfo
     * 本地访问路径：http://localhost:8111/repair/getRepairInfo
     * 参数：
     * {"repairCode":"REPAIR20241112141825"}
     */
    @ApiOperation(value = "获取维修结果", notes = "参数：{\"repairCode\":\"REPAIR20241112141825\"}", response = RepairResp.class)
    @RequestMapping(value = "/getRepairInfo",method = RequestMethod.POST, produces="text/html;charset=UTF-8")
    @ResponseBody
    public String getRepairInfo(@RequestBody RepairRequest request){
        String result;

        RepairBean repairBean = new RepairBean();
        repairBean.setPsRepairCode(request.getRepairCode());

        try {
            RepairBean returnBean = repairService.findOneByCode(repairBean);
            if (null != returnBean){
                RepairResp resp = new RepairResp();
                resp.setRepairCode(returnBean.getPsRepairCode());
                resp.setCustomerCode(returnBean.getPsCustomerCode());
                resp.setFeedbackCode(returnBean.getPsFeedbackCode());
                resp.setWorkOrderCode(returnBean.getPsWorkOrderCode());
                resp.setRepairContent(returnBean.getPsRepairContent());
                resp.setRepairVideoFile(returnBean.getPsRepairVideoFile());
                resp.setRepairVoiceFile(returnBean.getPsRepairVoiceFile());
                resp.setRepairImgFile(returnBean.getPsRepairImgFile());

                Map map = new HashMap();
                map.put("data", resp);
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_SUCCESS, "获取维修结果成功", map);
            }else{
                result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL,"暂无数据",new HashMap());
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = RespenseJsonTransformationUtil.jsonStr(ResultCode.CODE_FAIL, e.getMessage(),new HashMap());
        }
        return result;
    }

}
